package com.car.controller;

import com.car.pojo.MaintenanceAppointment;
import com.car.pojo.MaintenanceAppointmentQueryParam;
import com.car.pojo.Result;
import com.car.service.MaintenanceAppointmentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 保养预约管理Controller
 */
@Slf4j
@RequestMapping("/appointments/maintenance")
@RestController
public class MaintenanceAppointmentController {

    @Autowired
    private MaintenanceAppointmentService maintenanceAppointmentService;

    /**
     * 分页查询保养预约
     */
    @GetMapping
    public Result page(MaintenanceAppointmentQueryParam param) {
        log.info("分页查询保养预约: {}", param);
        return Result.success(maintenanceAppointmentService.page(param));
    }

    /**
     * 根据ID查询保养预约详情
     */
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Long id) {
        log.info("根据ID查询保养预约详情: {}", id);
        MaintenanceAppointment appointment = maintenanceAppointmentService.getInfo(id);
        return Result.success(appointment);
    }

    /**
     * 更新预约状态
     */
    @PutMapping("/status")
    public Result updateStatus(@RequestParam Long id, @RequestParam String status) {
        log.info("更新保养预约状态: id={}, status={}", id, status);
        maintenanceAppointmentService.updateStatus(id, status);
        return Result.success();
    }

    /**
     * 批量删除保养预约
     */
    @DeleteMapping
    public Result delete(@RequestParam List<Long> ids) {
        log.info("批量删除保养预约: {}", ids);
        maintenanceAppointmentService.delete(ids);
        return Result.success();
    }

    /**
     * 查询所有保养预约（用于导出等）
     */
    @GetMapping("/list")
    public Result list(MaintenanceAppointmentQueryParam param) {
        log.info("查询所有保养预约数据: {}", param);
        List<MaintenanceAppointment> list = maintenanceAppointmentService.list(param);
        return Result.success(list);
    }
    
    /**
     * 添加保养预约（用户提交预约）
     */
    @PostMapping
    public Result add(@RequestBody MaintenanceAppointment appointment) {
        log.info("添加保养预约: {}", appointment);
        maintenanceAppointmentService.add(appointment);
        return Result.success();
    }
    
    /**
     * 根据用户ID查询保养预约
     */
    @GetMapping("/user")
    public Result listByUserId(@RequestParam Long userId) {
        log.info("根据用户ID查询保养预约: userId={}", userId);
        List<MaintenanceAppointment> appointments = maintenanceAppointmentService.listByUserId(userId);
        return Result.success(appointments);
    }
    
    /**
     * 根据用户ID统计保养预约数量
     */
    @GetMapping("/user/count")
    public Result countByUserId(@RequestParam Long userId) {
        log.info("根据用户ID统计保养预约数量: userId={}", userId);
        Integer count = maintenanceAppointmentService.countByUserId(userId);
        return Result.success(count);
    }
    

}